package com.blog.blog.controller;

import com.blog.blog.common.ApiBaseResponse;
import com.blog.blog.service.AlertService;
import com.blog.blog.service.SystemMonitorService;
import com.blog.blog.util.annotations.RequirePerm;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 系统监控控制器
 */
@Tag(name = "系统监控", description = "系统监控和告警管理")
@RestController
@RequestMapping("/api/admin/monitor")
@RequiredArgsConstructor
public class AdminMonitorController {
    
    private final SystemMonitorService systemMonitorService;
    private final AlertService alertService;
    
    @Operation(summary = "获取系统信息")
    @GetMapping("/system")
    @RequirePerm({"system:monitor"})
    public ApiBaseResponse<Map<String, Object>> getSystemInfo() {
        return ApiBaseResponse.success(systemMonitorService.getSystemInfo());
    }
    
    @Operation(summary = "获取JVM信息")
    @GetMapping("/jvm")
    @RequirePerm({"system:monitor"})
    public ApiBaseResponse<Map<String, Object>> getJvmInfo() {
        return ApiBaseResponse.success(systemMonitorService.getJvmInfo());
    }
    
    @Operation(summary = "获取数据库信息")
    @GetMapping("/database")
    @RequirePerm({"system:monitor"})
    public ApiBaseResponse<Map<String, Object>> getDatabaseInfo() {
        return ApiBaseResponse.success(systemMonitorService.getDatabaseInfo());
    }
    
    @Operation(summary = "获取Redis信息")
    @GetMapping("/redis")
    @RequirePerm({"system:monitor"})
    public ApiBaseResponse<Map<String, Object>> getRedisInfo() {
        return ApiBaseResponse.success(systemMonitorService.getRedisInfo());
    }
    
    @Operation(summary = "获取磁盘信息")
    @GetMapping("/disk")
    @RequirePerm({"system:monitor"})
    public ApiBaseResponse<Map<String, Object>> getDiskInfo() {
        return ApiBaseResponse.success(systemMonitorService.getDiskInfo());
    }
    
    @Operation(summary = "检查系统健康状态")
    @GetMapping("/health")
    @RequirePerm({"system:monitor"})
    public ApiBaseResponse<Map<String, Object>> checkHealth() {
        return ApiBaseResponse.success(systemMonitorService.checkHealth());
    }
    
    @Operation(summary = "获取性能指标")
    @GetMapping("/performance")
    @RequirePerm({"system:monitor"})
    public ApiBaseResponse<Map<String, Object>> getPerformanceMetrics() {
        return ApiBaseResponse.success(systemMonitorService.getPerformanceMetrics());
    }
    
    @Operation(summary = "获取综合监控信息")
    @GetMapping("/overview")
    @RequirePerm({"system:monitor"})
    public ApiBaseResponse<Map<String, Object>> getMonitorOverview() {
        Map<String, Object> overview = new java.util.HashMap<>();
        overview.put("system", systemMonitorService.getSystemInfo());
        overview.put("jvm", systemMonitorService.getJvmInfo());
        overview.put("database", systemMonitorService.getDatabaseInfo());
        overview.put("redis", systemMonitorService.getRedisInfo());
        overview.put("disk", systemMonitorService.getDiskInfo());
        overview.put("health", systemMonitorService.checkHealth());
        overview.put("performance", systemMonitorService.getPerformanceMetrics());
        return ApiBaseResponse.success(overview);
    }
    
    @Operation(summary = "手动触发健康检查告警")
    @PostMapping("/alert/health")
    @RequirePerm({"system:monitor"})
    public ApiBaseResponse<String> triggerHealthAlert() {
        alertService.checkAndSendHealthAlerts();
        return ApiBaseResponse.success("健康检查告警已触发");
    }
    
    @Operation(summary = "发送测试告警")
    @PostMapping("/alert/test")
    @RequirePerm({"system:monitor"})
    public ApiBaseResponse<String> sendTestAlert() {
        Map<String, Object> details = new java.util.HashMap<>();
        details.put("测试时间", java.time.LocalDateTime.now().toString());
        details.put("测试用户", "管理员");
        
        alertService.sendAlert("INFO", "系统告警测试", "这是一条测试告警消息", details);
        return ApiBaseResponse.success("测试告警已发送");
    }
}
